const util = require('../utils/js/util')
const domain = 'https://jk.demo-dev.cn'

/**
 * 请求方法
 * @param {string} url 请求地址
 * @param {object} data 请求参数
 * @returns {Promise<object>} 返回结果
 */
 const request = (url, method, data = {}) => new Promise((resolve, reject) => {
    wx.request({
        data,
        url: `${domain}${url}`,
        method,
        header: { 'content-type': 'application/json', 'token': getApp().globalData?.userInfo?.token || '' },
        success: res => {
            if (res.statusCode === 200 && res.errMsg === "request:ok") {
                res.data.code === 1 || res.data.code === 200 ? resolve(res.data.data) : (reject(res.data), data?.nopop || util.toast(res.data.msg))
            } else {
                reject(res)
                data?.nopop || util.toast('网络错误，请联系管理员')
            }
        },
        fail: res => (reject(res), data?.nopop || util.toast('网络错误，请联系管理员'))
    })
})

// 登录
exports.login = data => request('/api/user/wxAppLogin', 'POST', data)

// 获取用户二维码
exports.getUserQRCode = () => new Promise((resolve, reject) => {
    wx.request({
        url: `${domain}/api/system/getUserQRCode`,
        method: 'GET',
        header: { 'content-type': 'application/json', 'token': getApp().globalData?.userInfo?.token || '' },
        success: res => res.errMsg === "request:ok" && res.statusCode === 200 ? resolve(res.data) : reject(res)
    })
})

// 修改用户信息
exports.profile = data => request('/api/user/profile', 'POST', data)

// 获取手机号码
exports.getMobile = data => request('/api/user/getMobile', 'POST', data)

// 获取banner
exports.getBannerList = data => request('/api/system/getBannerList', 'GET', data)

// 获取首页疾病类型 GET
exports.getHomeDiseaseList = data => request('/api/System/getHomeDiseaseList', 'GET', data)

// 获取健康服务列表 GET
exports.getServiceList = data => request('/api/service/getServiceList', 'GET', data)

// 获取服务信息 GET
exports.getService = data => request('/api/service/getService', 'GET', data)

// 获取检查项列表 GET
exports.getCheckList = data => request('/api/check/getCheckList', 'GET', data)

// 获取报告列表
exports.getReportList = data => request('/api/report/getReportList', 'GET', data)

// 换绑就诊人
exports.updateOrder = data => request('/api/service/updateOrder', 'GET', data)

// 换绑体检人
exports.checkUpdateOrder = data => request('/api/check/updateOrder', 'GET', data)

// 就诊人列表
exports.getClientList = data => request('/api/client/getClientList', 'GET', data)

// 更新就诊人
exports.updateClient = data => request('/api/client/updateClient', 'POST', data)

// 新增就诊人 POST
exports.addClient = data => request('/api/client/addClient', 'POST', data)

// 订单查询 GET
exports.getOrderList = data => request('/api/service/getOrderList', 'GET', data)

// 根据选中检查项，生成检查方案 POST
exports.analysis = data => request('/api/Check/analysis', 'POST', data)

// 删除就诊人
exports.delClient = data => request('/api/client/delClient', 'POST', data)

// 生成健康服务订单
exports.addOrder = data => request('/api/service/addOrder', 'POST', data)

// 支付
exports.pay = data => request('/api/system/pay', 'GET', data)

// 健康服务订单详情
exports.getOrder = data => request('/api/service/getOrder', 'GET', data)

// 生成检查订单
exports.addOrder_check = data => request('/api/check/addOrder', 'POST', data)

// 体检订单详情
exports.getOrderCheck = data => request('/api/check/getOrder', 'GET', data)

// 体检订单查询
exports.getOrderListCheck = data => request('/api/check/getOrderList', 'GET', data)

// 健康服务订单查询
exports.getOrders = data => request('/api/service/getOrderList', 'GET', data)